import { defineStore } from "pinia";
import type {IOrderGoods} from '@/types/type'

export const useOrderStore =defineStore('order',{
    state() {
        return{
            OrderList:[] as Array<IOrderGoods>
        }
    },
    actions:{
        test(testList:[]){
            this.OrderList = testList
        },
        deleteGoods(id: number) {
            const index = this.OrderList.findIndex(item => item.id === id)
            this.OrderList.splice(index, 1)
        },
    },
    getters:{
    // 总价计算
        totalPrice(): number {
            const sum = this.OrderList.reduce(
                (preventvalue, currentvalue) =>
                    preventvalue +
                    (currentvalue.state
                        ? currentvalue.price * currentvalue.num
                        : 0),
                0
            )
            return Number(sum.toFixed(2)) * 100
        }
    },
    persist: {
        enabled: true,//允许存储,默认true
        strategies: [
            {
                key: 'order',//存储的数据名字
                storage: localStorage,//保存的类型，默认是session,
                paths: ['OrderList'],//需要持久保存的state里面的数据
            },
        ],
    }
})